import 'dart:async';

import 'package:flutter/services.dart';

class MediaPicker {
  static const MethodChannel _channel = const MethodChannel('media_picker');

  /// [isEnableCrop] 是否开启裁剪
  /// [isCompress] 是否开启压缩
  /// [compressQuality] 压缩质量
  /// [isCamera] 是否显示相机
  static Future<List<MediaResult>> pickerImage(
      {isEnableCrop = true,
      isCompress = true,
      compressQuality = 80,
      isCamera = true,
      isGif = false,
      circleDimmedLayer = false,
      maxNumber = 1}) async {
    if (maxNumber > 1) {
      isEnableCrop = false;
    }

    var results = await _channel.invokeMethod<List>('chooseImg', {
      "isCrop": isEnableCrop,
      "isCamera": isCamera,
      "max": maxNumber,
      "isGif": isGif,
      "isCircle": circleDimmedLayer,
      "isCompress": isCompress,
      "quality": compressQuality
    });
    var list = results?.map((e) => MediaResult.fromJson(e)).toList() ?? [];
    return Future.value(list);
  }

  static Future<List<MediaResult>> pickerVideo({maxNum = 1}) async {
    var results = await _channel.invokeMethod<List>('chooseVideo', {
      "maxNum": maxNum,
    });
    var list = results?.map((e) => MediaResult.fromJson(e)).toList() ?? [];
    return Future.value(list);
  }

  static Future<List<MediaResult>> pickerAudio() async {
    var results = await _channel.invokeMethod<List>('chooseAudio');
    var list = results?.map((e) => MediaResult.fromJson(e)).toList() ?? [];
    return Future.value(list);
  }
}

class MediaResult {
  String? path;
  int? size;
  int? height;
  int? width;
  // 本地标识符 仅iOS
  String? localIdentifier;

  MediaResult(this.path, this.size, this.height, this.width,
      {this.localIdentifier});

  factory MediaResult.fromJson(Map json) {
    return MediaResult(
      json['path'],
      json['size'],
      json['height'],
      json['width'],
      localIdentifier: json['localIdentifier'],
    );
  }

  @override
  String toString() {
    return 'MediaResult{path: $path, size: $size, height: $height, width: $width, localIdentifier: $localIdentifier}';
  }
}
